﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using BTCe;
using BTCe.Constant;

namespace BTCe.Common
{
    public class PublicExchange
    {
        public const string DATATABLE_BID = "BIDTABLE";
        public const string DATATABLE_ASK = "ASKTABLE";
        public const string DATATABLE_OPENORDER = "OPENORDERTABLE";

        BTCeAccessInfo _AccessInfo;

        DataSet _dsInformation;
        DataTable _dtBidTable;
        DataTable _dtAskTable;
        DataTable _dtOpenOrderTable;


        public PublicExchange(BTCeAccessInfo AccesInfo)
        {
            _AccessInfo = AccesInfo;

            initializedDataSet();
        }

        public void initializedDataSet()
        {
            DataColumn dt_col;

            //DataSet 생성
            _dsInformation = new DataSet("BTCe");

            //Bid Table
            _dtBidTable = new DataTable(DATATABLE_BID);

            dt_col = _dtBidTable.Columns.Add();
            dt_col.ColumnName = "BidVolume";
            dt_col.DataType = System.Type.GetType("System.Decimal");

            dt_col = _dtBidTable.Columns.Add();
            dt_col.ColumnName = "BitcoinPrice";
            dt_col.DataType = System.Type.GetType("System.Decimal");

            dt_col = _dtBidTable.Columns.Add();
            dt_col.ColumnName = "AskVolume";
            dt_col.DataType = System.Type.GetType("System.Decimal");

            _dsInformation.Tables.Add(_dtBidTable);


            //Ask Table
            _dtAskTable = new DataTable(DATATABLE_ASK);

            dt_col = _dtAskTable.Columns.Add();
            dt_col.ColumnName = "BidVolume";
            dt_col.DataType = System.Type.GetType("System.Decimal");

            dt_col = _dtAskTable.Columns.Add();
            dt_col.ColumnName = "BitcoinPrice";
            dt_col.DataType = System.Type.GetType("System.Decimal");

            dt_col = _dtAskTable.Columns.Add();
            dt_col.ColumnName = "AskVolume";
            dt_col.DataType = System.Type.GetType("System.Decimal");

            _dsInformation.Tables.Add(_dtAskTable);

            //Order Table
            _dtOpenOrderTable = new DataTable(DATATABLE_OPENORDER);
            dt_col = _dtOpenOrderTable.Columns.Add();
            dt_col.ColumnName = "timestamp";
            dt_col.DataType = System.Type.GetType("System.TimeSpan");

            dt_col = _dtOpenOrderTable.Columns.Add();
            dt_col.ColumnName = "OrderID";
            dt_col.DataType = System.Type.GetType("System.Decimal");

            dt_col = _dtOpenOrderTable.Columns.Add();
            dt_col.ColumnName = "OrderType";
            dt_col.DataType = System.Type.GetType("System.String");

            dt_col = _dtOpenOrderTable.Columns.Add();
            dt_col.ColumnName = "BTCPrice";
            dt_col.DataType = System.Type.GetType("System.Decimal");

            dt_col = _dtOpenOrderTable.Columns.Add();
            dt_col.ColumnName = "BTCVolume";
            dt_col.DataType = System.Type.GetType("System.Decimal");

            dt_col = _dtOpenOrderTable.Columns.Add();
            dt_col.ColumnName = "OpenBtCVolume";
            dt_col.DataType = System.Type.GetType("System.Decimal");

            _dsInformation.Tables.Add(_dtOpenOrderTable);

        }

        public void RetreivedContract()
        {
            string strData = "";

            strData = Communication.GetRequestData(_AccessInfo.GetBaseURL() + BTCeURL.URL_ORDER_BOOKS);

            if (strData.Length == 0)
                return;

            SetContratData(strData);

        }

        private void SetContratData(string strData)
        {
            if (strData.Length == 0)
                return;

            //JSON으로 변경
            JObject objData = JObject.Parse(strData);

            JToken objSubData = objData["btc_usd"];

            // 매도 테이블 삽입
            JArray oItems = JArray.Parse(objSubData["asks"].ToString());
            _dtAskTable.Rows.Clear();
            foreach (JToken oItem in oItems)
            {
                try
                {
                    DataRow row = _dtAskTable.NewRow();
                    row["BitcoinPrice"] = oItem[0].ToString();
                    row["AskVolume"] = oItem[1].ToString();
                    _dtAskTable.Rows.Add(row);
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                }
            }

            // 매수 테이블 삽입
            oItems = JArray.Parse(objSubData["bids"].ToString());
            _dtBidTable.Rows.Clear();
            foreach (JToken oItem in oItems)
            {
                try
                {
                    DataRow row = _dtBidTable.NewRow();
                    row["BitcoinPrice"] = oItem[0].ToString();
                    row["BidVolume"] = oItem[1].ToString();
                    _dtBidTable.Rows.Add(row);
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                }

            }

        }

        public DataTable GetAskTable()
        {
            return _dtAskTable;
        }

        public DataTable GetBidTable()
        {
            return _dtBidTable;
        }

        public void RetreivedOpenOrder()
        {
            string strData = "";
            string strURL;
            string strParameters;
            
            //로그인 하지 않았다면
            if (_AccessInfo == null)
                return;
            if (!_AccessInfo.IsLogin())
                return;

            string strPair = "btc_usd";

            Dictionary<string, string> dicURLParameter = _AccessInfo.GetURLParameterAccessKey();
            dicURLParameter.Add("method", "ActiveOrders");
            dicURLParameter.Add("pair", strPair);


            //접속 주소를 입력한다.
            strURL = _AccessInfo.GetBaseURL() + BTCeURL.URL_ORDER_OPEN;
            strParameters = CommonFunction.ConvertURLParameter(dicURLParameter);

            strData = Communication.GetRequestData(strURL, strParameters, _AccessInfo.GetHttpHeaderAPI(strParameters));

            if (strData.Length == 0)
                return;

            SetOpenOrder(strData);

        }

        private void SetOpenOrder(string strData)
        {
            string strOrderID;

            if (strData.Length == 0)
                return;

            JObject objData = JObject.Parse(strData);
            objData = (JObject)objData["return"];

            _dtOpenOrderTable.Rows.Clear();

            if (objData == null)
                return;

            foreach (JProperty oObjItem in objData.Properties())
            {
                Console.WriteLine(oObjItem.Name);
                DataRow dr = _dtOpenOrderTable.NewRow();
                strOrderID = oObjItem.Name;

                JToken oSubItem = objData[strOrderID];

                if (oSubItem["type"].ToString() == "sell")
                {
                    dr["OrderType"] = "매도";
                }
                else
                {
                    dr["OrderType"] = "매수";
                }

                dr["OrderID"] = strOrderID;
                dr["BTCPrice"] = Double.Parse(oSubItem["rate"].ToString());
                dr["BTCVolume"] = Double.Parse(oSubItem["amount"].ToString());
                dr["timestamp"] = new TimeSpan(Int64.Parse(oSubItem["timestamp_created"].ToString()));

                _dtOpenOrderTable.Rows.Add(dr);

            }

        }

        public DataTable GetOpenOrderTable()
        {
            return _dtOpenOrderTable;
        }

    }
}
